<?php

namespace App\Http\Controllers\System;

use App\Package\Tree\Tree;
use App\Http\Controllers\Controller;
use App\Repositories\PermissionRepository;
use App\Http\Requests\System\Permission\QueryCollection;

/**
 * 权限管理控制器
 *
 * Date: 22/03/2018
 * @author George
 * @package App\Http\Controllers\System
 */
class PermissionController extends Controller
{
	protected $permissionRepository;

	/**
	 * PermissionController constructor.
	 * @param PermissionRepository $permissionRepository
	 */
	public function __construct(PermissionRepository $permissionRepository)
	{
		$this->permissionRepository = $permissionRepository;
	}

	/**
	 * 获取权限树
	 *
	 * Date: 22/03/2018
	 * @author George
	 * @param QueryCollection $request
	 * @param Tree $generator
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \App\Exceptions\GeneralException
	 */
	public function index(QueryCollection $request, Tree $generator)
	{
		$permissions = $this->permissionRepository->all();

		$tree = $generator->setPrimaryKey('code')->setParentKey('parent_code')->make($permissions);

		return success($tree);
	}
}
